package com.richesup.common.oss.common;

import com.richesup.common.oss.controller.ImageUploadController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;

import javax.xml.transform.Result;

/**
 * @Auther: yuhong
 * @Date: 2018-6-25 17:55
 * @Description:
 */
@ControllerAdvice
public class OSSExceptionHandler {
    private final static Logger logger= LogManager.getLogger(OSSExceptionHandler.class);



    @ExceptionHandler(value = {FileUploadBase.SizeLimitExceededException.class})
    @ResponseBody

    public ResultEntity SizeLimitExceededExceptionHandler(FileUploadBase.SizeLimitExceededException ex){

        logger.error(ex.toString());
        return  ResultEntity.error(CodeMessage.IMAGE_EXCEEDSLIMIT);
    }

    @ExceptionHandler(value = {MultipartException.class})
    @ResponseBody
    public ResultEntity MultipartExceptionHandler(MultipartException ex){

        logger.error(ex.toString());
        return  ResultEntity.error(CodeMessage.IMAGE_EXCEEDSLIMIT);
    }

    /**
     *
     * 功能描述:业务异常处理，直接返回错误信息
     *
     * @param: [ex]
     * @return: com.richesup.common.oss.common.ResultEntity
     * @auther: yuhong
     * @date: 2018-6-26 16:30
     */
    @ExceptionHandler(value = {OSSException.class})
    @ResponseBody
    public ResultEntity OSSExceptionHandler(OSSException ex){

        logger.error(ex.toString());
        return  ex.getResultEntity();
    }


    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public ResultEntity ExceptionHandler(Exception ex){
        logger.error(ex.toString());
        return  ResultEntity.error(CodeMessage.IMAGE_UNKNOWNERROR, ex.toString());
    }

}
